package com.cumt.threeway.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.cumt.threeway.R;
import com.cumt.threeway.activity.DetailActivity;
import com.cumt.threeway.activity.SubmitCommentActivity;
import com.cumt.threeway.adapter.CommentAdapter;
import com.cumt.threeway.adapter.NewCommentAdapter;
import com.cumt.threeway.constant.ThreeWayConstant;
import com.cumt.threeway.constant.URLS;
import com.cumt.threeway.model.Pinglun;
import com.cumt.threeway.model.Single;
import com.cumt.threeway.table.ActivitiesEntity;
import com.cumt.threeway.table.CommentEntity;
import com.cumt.threeway.util.ThreeWayData;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import java.util.List;


/**
 * Created by 胡晶晶 on 2016/5/22.
 */
public class CommentFragment extends Fragment implements View.OnClickListener{

    private Single mSingle;
    private Context mContext;
    private RecyclerView mRecyclerView;
    private ListView mListView;
    private RequestQueue queue;

    public static CommentFragment newInstance() {
        CommentFragment commentFragment = new CommentFragment();
        return commentFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_classify_baoming_pingjia, null, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        queue = Volley.newRequestQueue(getActivity());
        view.findViewById(R.id.btn_commit).setOnClickListener(this);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.lv_commit);
//        getDataFromInfo();
        getLocalData();
    }

    @Override
    public void onClick(View v) {
        if(v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.btn_commit:
                goSubmitCommentActivity();
                break;
        }
    }

    private void goSubmitCommentActivity() {
        Intent intent = new Intent(getActivity(), SubmitCommentActivity.class);
        intent.putExtra("name", ((DetailActivity)mContext).getActivitiesEntity().getName());
        getActivity().startActivityForResult(intent, ThreeWayConstant.INTENT_KEY_TO_LOGIN_2);
    }

    public void getLocalData() {
        ActivitiesEntity activitiesEntity = ((DetailActivity)mContext).getActivitiesEntity();
        List<CommentEntity> list = ThreeWayData.getInstance().getCommentInfo(activitiesEntity.getName());
        NewCommentAdapter newCommentAdapter = new NewCommentAdapter(list, getActivity());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(newCommentAdapter);
    }

    public void getDataFromInfo() {
        String url= URLS.IP + "NetServlet/GetData";
        //集合==》json数组
        final Gson g=new Gson();
        JsonArrayRequest jar;
        jar=new JsonArrayRequest(Request.Method.POST, url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                String data=response.toString();
                List<Pinglun> listStudents = g.fromJson(data, new TypeToken<List<Pinglun>>(){}.getType());
                CommentAdapter commentAdapter = new CommentAdapter(listStudents, getActivity());
                mListView.setAdapter(commentAdapter);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError arg0) {
            }
        });
        queue.add(jar);
    }
}
